package com.ewhale.feitengguest.utils

import cn.jiguang.share.android.api.JShareInterface
import cn.jiguang.share.qqmodel.QQ
import cn.jiguang.share.wechat.Wechat
import com.ewhale.feitengguest.app.AppApplication
import com.ewhale.feitengguest.dto.LoginInfoDto
import com.ewhale.feitengguest.receiver.TagAliasOperatorHelper
import com.ewhale.feitengguest.receiver.TagAliasOperatorHelper.TagAliasBean
import com.orhanobut.hawk.Hawk
import com.simga.library.utils.HawkKey

/**
 * 作者：SimGa
 * 日期：2020/6/27
 * 时间：14:30
 * 包名：com.ewhale.feitengguest.utils
 */
object UserInfoUtils {
    //    保存用户信息
    fun saveLoginUserInfo(userType:String,result: LoginInfoDto) {
        if ("我是用户" == userType) { //普通用户
            Hawk.put(HawkKey.USER_AVATAR, result.headPhoto)
            Hawk.put(HawkKey.USER_NICK_NAME, result.nickName)
            Hawk.put(HawkKey.USER_PHONE, result.phone)
            Hawk.put(HawkKey.USER_HX_ID, result.hxId)
            Hawk.put(HawkKey.USER_HX_PASSWORD, result.hxPassword)
            Hawk.put(HawkKey.USER_AUTH_STATUS, if (result.authStatus == 1) 2 else result.authStatus)
            Hawk.put(HawkKey.USER_LOGIN_INFO, result)
            Hawk.put(HawkKey.IS_USER_LOGIN, true)
            Hawk.put(HawkKey.NOW_APP_STATUS, Contants.USER_STATUS)
            Hawk.put(HawkKey.USER_AUTHENTICATION, result.sessionId)
        } else { //商家
            Hawk.put(HawkKey.SHOP_AVATAR, result.headPhoto)
            Hawk.put(HawkKey.SHOP_NICK_NAME, result.nickName)
            Hawk.put(HawkKey.SHOP_PHONE, result.phone)
            Hawk.put(HawkKey.SHOP_AUTH_STATUS, result.authStatus)
            Hawk.put(HawkKey.SHOP_LOGIN_INFO, result)
            Hawk.put(HawkKey.IS_SHOP_LOGIN, true)
            Hawk.put(HawkKey.LOGIN_SHOP_TYPE, result.shopType)
            Hawk.put(HawkKey.NOW_APP_STATUS, Contants.SHOP_STATUS)
            Hawk.put(HawkKey.SHOP_AUTHENTICATION, result.sessionId)
        }
    }

    /**
     *   退出登录。删除用户信息
     *   type 1 为退出用户端，2 为退出商家端
     */
    fun deleteUserInfo(type: Int) {
        //注销第三方登录
        if (Hawk.get(HawkKey.LOGIN_THIRD_TYPE, "") == Contants.weChat) {
            JShareInterface.removeAuthorize(Wechat.Name, null)
        } else if (Hawk.get(HawkKey.LOGIN_THIRD_TYPE, "") == Contants.qq) {
            JShareInterface.removeAuthorize(QQ.Name, null)
        }
        //退出极光
        val tagAliasBean = TagAliasBean()
        tagAliasBean.action = TagAliasOperatorHelper.ACTION_DELETE
        TagAliasOperatorHelper.sequence++
        tagAliasBean.alias = ""
        tagAliasBean.isAliasAction = true
        TagAliasOperatorHelper.getInstance().handleAction(AppApplication.sApplicationContext, TagAliasOperatorHelper.sequence, tagAliasBean)
        when (type) {
            1 -> {
//                退出IM的登录
//                if (!intent.getAction().equals("USER_LOGIN_ANOTHER_DEVICE")) {
//                AuthHelper.getInstance().logout(mContext, null);
//                }
                Hawk.delete(HawkKey.USER_AVATAR)
                Hawk.delete(HawkKey.USER_NICK_NAME)
                Hawk.delete(HawkKey.USER_PHONE)
                Hawk.delete(HawkKey.USER_AUTH_STATUS)
                Hawk.delete(HawkKey.USER_LOGIN_INFO)
                Hawk.delete(HawkKey.IS_USER_LOGIN)
                Hawk.delete(HawkKey.NOW_APP_STATUS)
                Hawk.delete(HawkKey.USER_AUTHENTICATION)
            }
            2 ->{
                Hawk.delete(HawkKey.SHOP_NICK_NAME)
                Hawk.delete(HawkKey.SHOP_PHONE)
                Hawk.delete(HawkKey.SHOP_AUTH_STATUS)
                Hawk.delete(HawkKey.SHOP_LOGIN_INFO)
                Hawk.delete(HawkKey.IS_SHOP_LOGIN)
                Hawk.delete(HawkKey.NOW_APP_STATUS)
                Hawk.delete(HawkKey.SHOP_AUTHENTICATION)
            }
        }
    }
}